<template>
  <div class="app-container">
    <Ht-search-form
      :default-event-key="defaultEventKey"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :advOptions="advOptions"
      permission="user:dictInfo:view"
    ></Ht-search-form>

    <ht-table
      ref="dataTable"
      :default-event-key="defaultEventKey"
      :multi-select="true"
      :dictMap="dictMap"
      :tableBindColumns="tableBindColumns"
      :queryParams="queryParams"
      :opButtonList="opButtonList"
      :reqConfig="regConfig"
    >
      <template v-slot:operate>
        <el-table-column
          :label="$t('common.operate')"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: $t('common.edit'),
                  permission: 'user:dictInfo:edit',
                  doFunc: () => {
                    handleAction('update', scope.row);
                  },
                },
                {
                  label: $t('common.delete'),
                  permission: 'user:dictInfo:remove',
                  doFunc: () => {
                    handleAction('del', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>
    <type-info
      v-if="open"
      :typeId="curTypeInfoId"
      :title="title"
      :open="open"
      @changeTypeInfoOpen="changeTypeInfoOpen"
    ></type-info>
  </div>
</template>

<script>
import { listPageMixin } from "@/libs/listPageMixin";
import { apiUrlConfig, delType } from "@/api/authority_app/dict/type";
import TypeInfo from "./typeInfo";

export default {
  name: "Dict",
  components: {
    TypeInfo,
  },
  mixins: [listPageMixin],
  data() {
    return {
      defaultEventKey: "dictInfoTableKey",
      // 查询参数
      queryParams: {
        dictName: "",
        dictType: "",
        status: "",
        beginTime: "",
        endTime: "",
      },
      // 搜索
      formOptions: [],
      advOptions: [],
      //
      // searchFormDictList: ["sys_normal_disable"],
      // 列信息
      regConfig: apiUrlConfig,
      tableBindColumns: [
        {
          label: this.$t("authority.dictInfo.form.dictName"),
          prop: "dictName",
        },
        {
          label: this.$t("authority.dictInfo.form.dictType"),
          prop: "dictType",
        },
        {
          label: this.$t("authority.dictInfo.form.status"),
          prop: "status",
          dictCode: "sys_normal_disable",
        },
        {
          label: this.$t("authority.dictInfo.form.remark"),
          prop: "remark",
          formatter: (row, value, index) => {
            if (!value) {
              return "-";
            } else {
              return value;
            }
          },
        },
        {
          label: this.$t("authority.dictInfo.form.createTime"),
          prop: "createTime",
          width: "180",
        },
      ],
      opButtonList: () => {
        return [
          {
            label: this.$t("common.add"),
            type: "primary",
            icon: "el-icon-plus",
            permission: "user:dictInfo:add",
            clickFun: () => {
              this.handleAction("add");
            },
          },
          // {
          //   label: "修改",
          //   type: "success",
          //   icon: "el-icon-edit",
          //   permission: "user:dictInfo:edit",
          //   disabled: !(this.ids.length == 1),
          //   clickFun: () => {
          //     this.handleAction("edit");
          //   }
          // },
          {
            label: this.$t("common.delete"),
            type: "danger",
            icon: "el-icon-delete",
            permission: "user:dictInfo:remove",
            actionType: "del",
          },
        ];
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      curTypeInfoId: "",
    };
  },
  mounted() {
    const formOptions = [
      // 普通搜索
      {
        type: "input",
        prop: "dictName",
        defaultAttr: {
          size: "small",
          label: this.$t("authority.dictInfo.form.dictName"),
          placeholder: this.$t("authority.dictInfo.rules.dictNamePLH"),
        },
      },
      {
        type: "input",
        prop: "dictType",
        defaultAttr: {
          size: "small",
          label: this.$t("authority.dictInfo.form.dictType"),
          placeholder: this.$t("authority.dictInfo.rules.dictTypePLH"),
        },
      },
      {
        type: "dict",
        dict: "sys_normal_disable",
        prop: "status",
        selectOptionList: [],
        defaultAttr: {
          size: "small",
          label: this.$t("authority.dictInfo.form.status"),
          placeholder: this.$t("authority.dictInfo.rules.statusPLH"),
        },
      },
    ];
    const advOptions = [
      {
        type: "dates",
        nameObj: {
          startTimeName: "beginTime",
          endTimeName: "endTime",
        },
        defaultAttr: {
          size: "small",
          label: this.$t("authority.dictInfo.form.createDate"),
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd",
          placeholder: [
            this.$t("authority.dictInfo.rules.startDatePLH"),
            this.$t("authority.dictInfo.rules.endDatePLH"),
          ],
        },
      },
    ];
    this.formOptions = formOptions;
    this.advOptions = advOptions;
  },

  methods: {
    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "add":
          // this.$refs.editConfigDialog.show();
          this.handleAdd();
          break;
        // case "edit":
        //   this.handleEdit();
        //   break;
        case "del":
          this.handleDelete(row);
          // const id = row.id || this.ids.join(",");
          // this.$confirm(
          //   `是否确认删除 ${row ? "这" : ids.split(",").length} 个配置数据?`,
          //   "警告",
          //   {
          //     confirmButtonText: "确定",
          //     cancelButtonText: "取消",
          //     type: "warning"
          //   }
          // )
          //   .then(function() {
          //     return delType(id);
          //   })
          //   .then(() => {
          //     this.doQuery();
          //     this.msgSuccess("删除成功");
          //   })
          //   .catch(action => {});
          break;
        case "update":
          // this.$refs.editConfigDialog.show(row.id);
          this.handleUpdate(row);
          break;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.curTypeInfoId = "";
      this.open = true;
      this.title = this.$t("authority.dictInfo.form.addDictType");
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.curTypeInfoId = row.id;
      this.open = true;
      this.title = this.$t("authority.dictInfo.form.editDictType");
    },

    /** 修改按钮操作 */
    handleEdit() {
      this.curTypeInfoId = this.ids[0];
      this.open = true;
      this.title = this.$t("authority.dictInfo.form.updateDictType");
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.id || this.ids;
      let confirmText = "";
      if (this.ids.length == 0) {
        confirmText = this.$t("authority.dictInfo.rules.deleteTips");
      } else {
        confirmText = this.$t("authority.dictInfo.rules.multiDeleteTip", {
          val: this.ids.length,
        });
      }
      this.$confirm(confirmText, this.$t("authority.dictInfo.form.warning"), {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning",
      })
        .then(function () {
          return delType(dictIds);
        })
        .then(() => {
          this.doQuery(this.defaultEventKey);
          this.msgSuccess(this.$t("common.deleteSuccess"));
        })
        .catch((action) => {});
    },
    //一级弹窗状态修改
    changeTypeInfoOpen(open) {
      this.open = open;
      if (!this.open) {
        this.doQuery(this.defaultEventKey);
      }
    },
  },
};
</script>
